<?php

namespace application\member\controller;

class import extends \application\common\controller\admin {

    //首
    public function index() {
        if (submitcheck("dosubmit")) {


            $filename = $_FILES['file']['tmp_name'];
            if (empty($filename)) {
                echo '请选择要导入的CSV文件！';
                exit;
            }
            $handle = fopen($filename, 'r');
            $result = $this->input_csv($handle); //解析csv
            $len_result = count($result);
            if ($len_result == 0) {
                echo '没有任何数据！';
                exit;
            }
            debug($result);
        } else {
            $this->display('import/import_index');
        }
    }

    public function export() {
        $this->display('import/export_index');
    }

    //TODO:CSV字段分割问题
    private function input_csv($handle) {
        $out = array();
        $n = 0;
        while ($data = fgetcsv($handle, 10000)) {
            $num = count($data);
            for ($i = 0; $i < $num; $i++) {
                $out[$n][$i] = $data[$i];
            }
            $n++;
        }
        return $out;
    }

}
